﻿Imports VendasBLL
Public Class ConsultaPlacasFRM
    Dim ObjBLL As New PlacaBLL
    Public Tipo As String
    Public ObjP As PlacaBLL

#Region "Métodos"
#Region "Limpa"
    Sub Limpa()
        TXTLocalizar.Text = ""
        CBFiltro.SelectedIndex = 1
    End Sub

#End Region

#Region "Carrega grid"
    Sub CarregaGrid()
        DGVDados.DataSource = ObjBLL.buscar("", 7)
        DGVDados.Refresh()
    End Sub
#End Region

#End Region

#Region "Botoes"
#Region "Voltar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        ObjP = Nothing
        Me.Close()
    End Sub
#End Region

#Region "Cadastrar"
    Private Sub TSBCadastrar_Click(sender As System.Object, e As System.EventArgs) Handles TSBCadastrar.Click
        PlacaFRM.Op = "I"
        PlacaFRM.ShowDialog()
        CarregaGrid()
    End Sub
#End Region

#End Region

#Region "Eventos"
#Region "ComboBox Filtro"
    Private Sub CBFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBFiltro.KeyPress
        e.Handled = True
    End Sub
#End Region

#Region "Localizar no textbox"
    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
        If TXTLocalizar.Text = "" Then
            DGVDados.DataSource = ObjBLL.buscar("", 7)
        End If
        DGVDados.Refresh()
    End Sub

#End Region

#Region "Keypress Localizar"
    Private Sub TXTLocalizar_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTLocalizar.KeyPress, TXTLocalizar.KeyPress
        If CBFiltro.SelectedIndex = 0 Then
            If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
                e.Handled = True
            End If
        End If
    End Sub
#End Region

#Region "ComboboxFiltro SelectedIndex"
    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        If CBFiltro.SelectedIndex = 4 Then
            CBStatus.Visible = True
            TXTLocalizar.Visible = False
            CBStatus.SelectedIndex = 0
            CBStatus_SelectedIndexChanged(sender, e)
        Else
            CBStatus.Visible = False
            TXTLocalizar.Visible = True
            CarregaGrid()
        End If
        TXTLocalizar.Text = ""

    End Sub
#End Region

#Region "ComboboxStatus Selectedindex"
    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        DGVDados.DataSource = ObjBLL.buscar(CBStatus.Text, CBFiltro.SelectedIndex)
    End Sub
#End Region

#Region "Double click do datagrid"
    Private Sub DGVDados_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellDoubleClick
        Try

            If Tipo = "ret" Then
                Dim indice As Integer = -1
                'Percorre O DataGridView Verificando a Linha selecionada
                For Each linha As DataGridViewRow In DGVDados.SelectedRows
                    If (Not linha.Cells("pla_codigo") Is Nothing) Then
                        'Guarda A Posição Selecionada
                        indice = linha.Index
                    End If
                Next
                If (indice <> -1) Then
                    If (DGVDados.Rows(indice).Cells("sta_codigo").Value = 1) Then
                        ObjP = New PlacaBLL(Val(DGVDados.Rows(indice).Cells("pla_codigo").Value.ToString()))

                        Me.Close()
                    Else
                        Throw New Exception("Placa inativa")
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
#End Region

#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region

#Region "Load"
    Private Sub ConsultaMarcaFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If TXTLocalizar.Text = "" Then
            CarregaGrid()
            Limpa()
        End If

    End Sub
#End Region

#Region "DatagridView enter"
    Private Sub DGVDados_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DGVDados.KeyPress
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            DGVDados_CellDoubleClick(Nothing, Nothing)
        End If
    End Sub
#End Region
#End Region

End Class